package com.sneakxy.mybase4j.platform.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sneakxy.mybase4j.base.bean.ValidAdd;
import com.sneakxy.mybase4j.base.bean.ValidEdit;
import com.sneakxy.mybase4j.base.controller.BaseController;
import com.sneakxy.mybase4j.platform.beans.Permission;
import com.sneakxy.mybase4j.platform.beans.Router;
import com.sneakxy.mybase4j.platform.service.IPermissionService;
import com.sneakxy.mybase4j.platform.service.IRouterService;
import com.sneakxy.mybase4j.util.BeanHelper;
import com.sneakxy.mybase4j.util.DataGrid;
import com.sneakxy.mybase4j.util.PageSearch;
import com.sneakxy.mybase4j.util.Response;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 路由管理
 * @author 潜行的青衣
 * @see
 */
@RestController
@RequestMapping("/api/router/")
public class RouterController extends BaseController {

	@Resource
	private IRouterService routerService;
	
	@Resource
	private IPermissionService permissionService;
	
	@RequiresPermissions("sys:router:manager")
	@RequestMapping("datagrid")
	public DataGrid<Router> datagrid(PageSearch search) {
		if(StringUtils.isBlank(search.getSortName())) {
			search.setSortName("createtime");
		}
		return new DataGrid<Router>(routerService.findBySearch(search.getSearchText(), search.toPageable()));
	}
	
	@RequiresPermissions("sys:router:manager:add")
	@RequestMapping("add")
	public Response add(@Validated(ValidAdd.class) Router router, BindingResult result) {
		if(result.hasErrors()) {
			return this.getFieldErrorsResponse(result);
		}
		
		if(!this.validateNameForAdd(router.getName())) {
			return new Response(false, 1);
		}
		
		//去空格
		BeanHelper.trimToEmpty(router);
		router.setPermId(null);
		router.setId(null);
		
		router = this.routerService.save(router);
		return new Response(true, router);
	}
	
	@RequiresPermissions("sys:router:manager:add")
	@RequestMapping("validateNameForAdd")
	public Boolean validateNameForAdd(String name) {
		CriteriaBuilder<Router> builder = new CriteriaBuilder<Router>(Router.class);
		builder.andEq("name", StringUtils.trimToEmpty(name));
		return this.routerService.count(builder.build()) == 0;
	}
	
	@RequiresPermissions("sys:router:manager:edit")
	@RequestMapping("edit")
	public Response edit(@Validated(ValidEdit.class) Router router, BindingResult result) {
		if(result.hasErrors()) {
			return this.getFieldErrorsResponse(result);
		}
		if(!this.validateNameForEdit(router.getId(), router.getName())) {
			return new Response(false, 1);
		}
		Router old = this.routerService.findOne(router.getId());
		//去空格
		BeanHelper.trimToEmpty(router);
		router.setPermId(old.getPermId());
		router.setCreatetime(old.getCreatetime());
		
		router = this.routerService.save(router);
		return new Response(true, router);
	}
	
	@RequiresPermissions("sys:router:manager:edit")
	@RequestMapping("validateNameForEdit")
	public Boolean validateNameForEdit(String id, String name) {
		CriteriaBuilder<Router> builder = new CriteriaBuilder<Router>(Router.class);
		builder.andNotEq("id", StringUtils.trimToEmpty(id));
		builder.andEq("name", StringUtils.trimToEmpty(name));
		return this.routerService.count(builder.build()) == 0;
	}
	
	@RequiresPermissions("sys:router:manager:remove")
	@RequestMapping("remove")
	public Response remove(String[] ids) {
		if(ids != null) {
			for(String id:ids) {
				this.routerService.delete(id);
			}
		}
		return new Response(true);
	}
	
	@RequiresPermissions("sys:router:manager:perm")
	@RequestMapping("tree")
	public Response tree() {
		List<Permission> list = this.permissionService.findAllByParentId(true);
		return new Response(list);
	}
	
	@RequiresPermissions("sys:router:manager:perm")
	@RequestMapping("perm")
	public Response perm(String id, String permId) {
		Router old = this.routerService.findOne(id);
		old.setPermId(StringUtils.trimToNull(permId));
		this.routerService.save(old);
		return new Response(true);
	}
	
}
